<template>
  <MyScroll class="list-view"
            :dataResource="singerData"
            :pullDown="true"
            :listenScroll="true"
            @onScroll="onScroll"
            ref="myScroll"
            @pulldown="pulldown">
    <ul class="singer-wrap"
        v-for="(singerList,index) in singerData"
        :key="index"
        ref="singerWrap">
      <li class="singer-wrap-title"
          :index='index'>
        {{singerList.title}}
      </li>
      <ul class="singer-list">
        <li class="singer-list-item"
            v-for="item in singerList.list"
            :key="item.id">
          <img :src="item.pic">
          <p>{{item.name}}</p>
        </li>
      </ul>
    </ul>
    <template v-slot:noScrol>
      <ul class="fixed"
          @touchstart.stop.prevent="onShortcutTouchStart">
        <li v-for="(item,index) in computeFixedText"
            :key="index"
            :data-index="index"
            class="fixed-item"
            :class="{'active': currentIndex === index}">{{item}}</li>
      </ul>
      <div class="list-title"
           v-show="isShowListTitle&&singerData.length">{{computeFixedText[currentIndex]}}</div>
    </template>
  </MyScroll>
</template>
<script>
import MyScroll from './myScroll.vue'
import { getAttrVal } from '@/assets/js/dom'
export default {
  props: {
    singerData: {
      type: Array,
      required: true,
      default: () => []
    }
  },
  data () {
    return {
      currentIndex: 0,
      isShowListTitle: true,
      diff: 0
    }
  },
  components: {
    MyScroll
  },
  computed: {
    computeFixedText () {
      const fixedArr = this.singerData.map(item => item.title)
      return fixedArr
    }
  },
  methods: {
    onScroll (pos) {
      const scrollHeight = -pos.y
      const heightArr = this.heightArr
      if (scrollHeight < 0) {
        this.currentIndex = 0
        this.isShowListTitle = false
        return
      }
      this.isShowListTitle = true
      for (var i = 0; i < heightArr.length; i++) {
        const height1 = heightArr[i]
        const height2 = heightArr[i + 1]
        if (scrollHeight >= height1 && scrollHeight < height2) {
          this.currentIndex = i
          this.diff = scrollHeight - heightArr[i + 1]
          return
        }
      }
      // 当滚动到底部，且-newY大于最后一个元素的上限
      this.currentIndex = heightArr.length - 2
    },
    getHeightArr () {
      if (!this.singerData.length) return
      const heightArr = [0]
      const singerWrapList = this.$refs.singerWrap
      this.singerWrapList = singerWrapList
      let totalHeight = 0
      singerWrapList && singerWrapList.forEach(item => {
        const height = item.clientHeight
        totalHeight += height
        heightArr.push(totalHeight)
      })
      this.heightArr = heightArr
    },
    onShortcutTouchStart (e) {
      const index = getAttrVal(e.target, 'index')
      this.$refs.myScroll.scrollToElement(this.$refs.singerWrap[index], 300)
    },
    pulldown (val) {
      this.$emit('pulldown', val)
    }
  },
  watch: {
    singerData () {
      this.$nextTick(() => {
        this.getHeightArr()
      })
    },
    diff (value) {
      // console.log(value)
    }
  }
}
</script>
<style scoped lang = 'scss'>
@import "@css/variable.scss";
.list-view {
  height: calc(100vh - 88px);
  overflow: hidden;
  .singer-wrap {
    .singer-wrap-title {
      height: 40px;
      line-height: 40px;
      padding: 0 20px;
      background-color: #ccc;
    }
    .singer-list {
      overflow: hidden;
      padding-bottom: 20px;
      .singer-list-item {
        display: flex;
        align-items: center;
        padding: 0 20px;
        margin-top: 20px;
        img {
          height: 40px;
          width: 40px;
          border-radius: 50%;
          margin-right: 40px;
        }
      }
    }
  }
  .fixed {
    width: 30px;
    height: 80vh;
    position: fixed;
    right: 0;
    top: 50%;
    transform: translate3d(0, -50%, 0);
    display: flex;
    flex-direction: column;
    justify-content: space-between;
    z-index: 100;
    &-item {
      text-align: center;
      height: 30px;
      line-height: 30px;
      cursor: pointer;
    }
    .active {
      color: $color-theme;
    }
  }
  .list-title {
    position: fixed;
    top: 88px;
    width: 100vw;
    height: 40px;
    line-height: 40px;
    padding-left: 20px;
    background-color: #ccc;
  }
}
</style>
